package com.spyx.product.api;


import com.atyx.spyx.model.product.Category;
import com.atyx.spyx.model.product.SkuInfo;
import com.atyx.spyx.vo.product.SkuInfoVo;
import com.atyx.spyx.vo.product.SkuStockLockVo;
import com.spyx.product.service.CategoryService;
import com.spyx.product.service.SkuInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//当前controller只在服务之间调取时使用
@RestController
@RequestMapping("/api/inner/product")
@Api(tags = "商品服务调取")
public class ProductInnerController {
    @Autowired
    private SkuInfoService skuInfoService;

    @ApiOperation("根据skuid,查询skuinfo信息")
    @GetMapping("/getskuinfobyid/{skuid}")
    public SkuInfo findById(@PathVariable("skuid")Long skuid){
        SkuInfo info = skuInfoService.getById(skuid);
        return info;
    }

    @Autowired
    private CategoryService categoryService;

    @ApiOperation("根据categoryid,查询category信息")
    @GetMapping("/getcategorybyid/{categoryid}")
    public Category getcategorybyid(@PathVariable("categoryid")Long categoryid){
        return categoryService.getById(categoryid);
    }


    //根据skuid集合，查询skuInfo列表
    //注意: 1.由于调取时传递了一个list集合，请求方式要使用post
    //     2.List集合在接收数据时使用@RequestBody注解，并添加泛型
    @ApiOperation("根据skuid集合，查询skuInfo列表")
    @PostMapping("/getskuInfoListbyids")
    public List<SkuInfo> getskuInfoListbyids(@RequestBody List<Long> ids){
       List<SkuInfo> skuInfos= skuInfoService.getSkuInfoList(ids);
        return skuInfos;
    }

    //添加活动范围时，根据关键字,查询商品列表
    @GetMapping("getSkuInfoListBykeyword/{keyword}")
    public List<SkuInfo> getSkuInfoListBykeyword(@PathVariable("keyword")String keyword){
       List<SkuInfo> list= skuInfoService.getSkuInfoListBykeyword(keyword);
        return list;
    }

    //根据id集合，查询出分类集合
    @ApiOperation("根据categoryid集合，查询category列表")
    @PostMapping("/getcategoryListbyids")
    public List<Category> getcategoryListbyids(@RequestBody List<Long> ids){
      List<Category> list=  categoryService.getcategoryListbyids(ids);
        return list;
    }

    //   微信小程序端代码开发
    @ApiOperation("小程序端查询所有分类列表")
    @GetMapping("inner/findAllCategoryList")
    public List<Category> getCategoryList(){
       List<Category> categories= categoryService.getAll();
       return categories;
    }

    //查询新人专享
    @ApiOperation("小程序端查询新人专享列表")
    @GetMapping("inner/findNewPersonSkuInfoList")
    public List<SkuInfo> getNewPersonSkuinfoList(){
       List<SkuInfo> list= skuInfoService.getNewPersonSkuinfoList();
        return list;
    }

    //根据skuid,查询SkuInfoVo的信息(根据商品id,查询商品信息)
    @GetMapping("inner/getSkuInfoBySkuId/{skuid}")
    public SkuInfoVo getSkuInfoBySkuId(@PathVariable("skuid")Long skuid){

        return skuInfoService.getSkuInfoBySkuId(skuid);
    }

    //检查并锁定库存
    @ApiOperation(value = "锁定库存")
    @PostMapping("inner/checkAndLock/{orderNo}")
    public Boolean checkAndLock(@RequestBody List<SkuStockLockVo> skuStockLockVoList,
                                @PathVariable String orderNo) {
        //这里的boolean表示锁定商品成功或失败
        Boolean rs= skuInfoService.checkAndLock(skuStockLockVoList, orderNo);
        return rs;
    }



}
